package customerEnter.Frame.oss;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;

import customerEnter.Frame.Utils.HelpClass;
import customerEnter.tool.WhetherToEmpty;

/**
 * 測試
 * 
 * 类名称：test 类描述： 创建人：dy 创建时间：2016-5-10 下午1:36:55
 * 
 */
@SuppressWarnings("unused")
public class OSS {
	private static OSSClient	ossClient	= getOssClient();
	private static OSS			oss;

	public static OSS getOss() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(oss)) {
			oss = new OSS();
		}
		return oss;
	}

	public static void main(String[] args) throws Exception {
		// 獲取鏈接
		// 獲取bucket下面的object list
		ObjectListing osListing = ossClient.listObjects(OSSUseTransmission.getAucketName());
		// 上傳文件並且返回 路徑osListing.getObjectSummaries().iterator()
		/*
		 * System.out.println(getOss().uploadFile(ossClient, " ", "systemImg\\"
		 * + DateUtils.getDay() + "\\" + DateUtils.getMoth() + "\\" +
		 * DateUtils.getDay()));
		 */
		System.out.println(OSSUseTransmission.getAucketName());

		getAllObjcetFile(getListObjects(ossClient, OSSUseTransmission.getAucketName()));// 獲取文件夾
		getAllObject(osListing);
		ossClient.shutdown();
	}

	/**
	 * 獲取OSS客戶端連接
	 * 
	 * @return
	 */
	public static OSSClient getOssClient() {
		ossClient = new OSSClient(OSSUseTransmission.getEndpoint(), OSSUseTransmission.getAccessKeyId(),
				OSSUseTransmission.getAccessKeySecret());
		return ossClient;
	}

	/**
	 * 獲取Bucket 下邊的所有object List信息
	 * 
	 * @return
	 */
	private static ObjectListing getObjectListing() {
		ObjectListing osListing = ossClient.listObjects(OSSUseTransmission.getAucketName());
		return osListing;
	}

	private static void setBucktCanned(OSSClient ossClient, String bucketName) {
		ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
	}

	/**
	 * 獲取object下面的文件
	 */
	public void getObjectFileAll(OSSClient ossClient, String bucketName, String ObjectName) {

	}

	private static ObjectListing getListObjects(OSSClient ossClient, String bucketName) {
		// 构造ListObjectsRequest请求
		ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
		// 设置参数
		listObjectsRequest.setDelimiter("/");
		ObjectListing listing = ossClient.listObjects(listObjectsRequest);
		return listing;
	}

	/**
	 * 查看全部的object
	 */
	public static void getAllObject(ObjectListing osListing) {
		System.out.println(" ");
		System.out.println("總共有Object:" + osListing.getObjectSummaries().size());
		// 查看全部的object
		for (OSSObjectSummary ossobj : osListing.getObjectSummaries()) {
			String key = ossobj.getKey();
			System.out.println(key);
		}
	}

	private static void getAllObjcetFile(ObjectListing osListing) {
		System.out.println(" ");
		System.out.println("總共有文件夾:" + osListing.getObjectSummaries().size());
		// 查看全部的object

		System.out.println(" ");
	}

	/**
	 * 刪除指定的object 要在object 名字後面加/
	 */
	public static void deleteObject(OSSClient ossClient, String bucketName, String objectName) {
		ossClient.deleteObject(bucketName, objectName);
	}

	/**
	 * 創建object 不用 在 object 後面加/
	 */
	public static void createObject(OSSClient ossClient, String ObjectName) {
		ossClient
				.appendObject(new AppendObjectRequest(OSSUseTransmission.getAucketName(), ObjectName, createTempFile())
						.withPosition(0L));
	}

	public static File createTempFile() {
		File file = null;
		try {
			file = File.createTempFile("oss-java-sdk-", ".txt");
			file.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

	/**
	 * LocalFilepath 本地文件路徑 上傳到哪裡
	 */
	public String uploadFile(OSSClient ossClient, String LocalFilepath, String ObjectPath) throws Exception {
		File file = new File(LocalFilepath);
		InputStream fileContent = null;
		fileContent = new FileInputStream(file);
		String ext = "." + HelpClass.getFileExt(file);

		String remoteFilePath = ObjectPath.substring(0, ObjectPath.length()).replaceAll("\\\\", "/") + "/";
		// 创建上传Object的Metadata
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(fileContent.available());
		objectMetadata.setCacheControl("no-cache");
		objectMetadata.setHeader("Pragma", "no-cache");
		objectMetadata.setContentType(contentType(file.getName().split("\\.")[1].toString()));
		String UUID = HelpClass.getImgName();
		objectMetadata.setContentDisposition("inline;filename=" + UUID + ext);
		// 上传文件
		ossClient.putObject(OSSUseTransmission.getAucketName(), remoteFilePath + UUID + ext, fileContent,
				objectMetadata);
		return "/" + remoteFilePath + UUID + ext;
	}

	/**
	 * 判斷文件類型
	 */
	public static String contentType(String FilenameExtension) {
		if (FilenameExtension.equals("BMP") || FilenameExtension.equals("bmp")) {
			return "image/bmp";
		}
		if (FilenameExtension.equals("GIF") || FilenameExtension.equals("gif")) {
			return "image/gif";
		}
		if (FilenameExtension.equals("JPEG") || FilenameExtension.equals("jpeg") || FilenameExtension.equals("JPG")
				|| FilenameExtension.equals("jpg") || FilenameExtension.equals("PNG")
				|| FilenameExtension.equals("png")) {
			return "image/jpeg";
		}
		if (FilenameExtension.equals("HTML") || FilenameExtension.equals("html")) {
			return "text/html";
		}
		if (FilenameExtension.equals("TXT") || FilenameExtension.equals("txt")) {
			return "text/plain";
		}
		if (FilenameExtension.equals("VSD") || FilenameExtension.equals("vsd")) {
			return "application/vnd.visio";
		}
		if (FilenameExtension.equals("PPTX") || FilenameExtension.equals("pptx") || FilenameExtension.equals("PPT")
				|| FilenameExtension.equals("ppt")) {
			return "application/vnd.ms-powerpoint";
		}
		if (FilenameExtension.equals("DOCX") || FilenameExtension.equals("docx") || FilenameExtension.equals("DOC")
				|| FilenameExtension.equals("doc")) {
			return "application/msword";
		}
		if (FilenameExtension.equals("XML") || FilenameExtension.equals("xml")) {
			return "text/xml";
		}
		return "text/html";
	}
}
